package com.hdit.guimei.service.impl;

import com.hdit.guimei.domain.Admin;
import com.hdit.guimei.mapper.AdminMapper;
import com.hdit.guimei.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @基本功能:
 * @ClassName: AdminServiceImpl
 * @Description: TODO
 * @Author: 李嘉明
 * @Date: 2021/1/18 10:20
 * @Version 1.0
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    /**
    * @Method: adminRegister
    * @函数(方法)功能:管理员注册
    * @Author: 李嘉明
    * @params:  [admin]
    * @Return: int
    * @Exception:
    * @Date: 2021/1/18 13:14
    */
    public int adminRegister(Admin admin) {
        return  adminMapper.addAdmin(admin);
    }

    @Override
    /**
    * @Method: adminLogin
    * @函数(方法)功能:管理员登录
    * @Author: 李嘉明
    * @params:  [admin_name]
    * @Return: com.hdit.guimei.domain.Admin
    * @Exception:
    * @Date: 2021/1/18 13:14
    */
    public int adminLogin(String admin_name,String admin_pwd) {
        Admin admin = adminMapper.findAdminByName(admin_name);
        //用户不存在
        if(admin==null){
            return 0;
        }
        //如果登录密码正确，返回1
        if(admin_pwd.equals(admin.getAdmin_pwd())){
            return 1;
        }
        //密码错误
        return -1;
    }

    @Override
    public int updateAdminPwd(String admin_name, String admin_pwd, String new_admin_pwd) {
        Admin admin = adminMapper.findAdminByName(admin_name);
        if(admin==null){
            return 2;
        }
        if (admin_pwd.equals(admin.getAdmin_pwd())){
            Map map = new HashMap<String,String>();
            map.put("admin_name",admin_name);
            map.put("new_admin_pwd",new_admin_pwd);
            return  adminMapper.updateAdminPwdByName(map);
        }
        //要修改的用户，输入密码错误
        return -1;
    }
}
